<template>
    <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
        <el-form class="tl-form" ref="dealform" :model="model" :reules="rules" label-width="110px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="付款日期" prop="paydate" :rules="rules.validateRequire">
                        <el-date-picker v-model="model.paydate" type="date" placeholder="付款日期" value-format="yyyy-MM-dd"></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="110px" :disabled="disableEditSubmit || disableSave">
            <el-row>
                <el-col :span="24">
                    <el-divider content-position="left"><span>【以下信息为只读信息】</span></el-divider>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-divider content-position="left"><span>1、【项目】相关信息</span></el-divider>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="项目名称">
                        <el-input v-model="model.dispprojectid" :disabled="true"> </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目编号">
                        <el-input v-model="model.projectcode" :disabled="true"> </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="采购合同金额">
                        <el-input-number v-model="model.projectpurchasecontractmoney" controls-position="right" :precision="2" :disabled="true" style="width:95%"> </el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目金额">
                        <el-input-number v-model="model.projectmoney" controls-position="right" :precision="2" :disabled="true" style="width:95%"> </el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="采购付款金额">
                        <el-input-number v-model="model.projectpurchasepaymentmoney" controls-position="right" :precision="2" :disabled="true" style="width:95%"> </el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="已回款金额">
                        <el-input-number v-model="model.projectrecbillmoney" controls-position="right" :precision="2" :disabled="true" style="width:95%"> </el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-divider content-position="left"><span>2、【合同】相关信息</span></el-divider>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="采购合同" prop="purchasecontractid" :required="true" :rules="rules.validateRequire">
                        <el-input v-model="model.disppurchasecontractid" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmPurchaseContractRefDialog" v-if="!(disableEditSubmit || disableSave)"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="采购合同金额">
                        <el-input-number v-model="model.contractmoney" controls-position="right" :precision="2" :disabled="true" style="width:95%"> </el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="已付款金额">
                        <el-input-number v-model="model.contracttotalpaymoney" controls-position="right" :precision="2" :disabled="true" style="width:95%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="采购内容">
                        <el-input v-model="model.contractcontent" :disabled="true" class="is-disabled"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <!-- <el-row>
                <el-col :span="12">
                    <el-form-item label="已付款金额">
                        <el-input-number v-model="model.contracttotalpaymoney" controls-position="right" :precision="2" :disabled="true" style="width:95%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
            </el-row> -->
            <el-row>
                <el-col :span="24">
                    <el-divider content-position="left"><span>3、【付款申请】相关信息</span></el-divider>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="付款部门" prop="purchasedeptcode" :required="true" :rules="rules.validateRequire">
                        <tl-ref-dept v-model="model.purchasedeptcode" :initDisplayData.sync="model.purchasedeptname" dataType="code" nameType="fullname" :singleSelect="true" @confirm="onSelectPurchasedept" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="付款组织" prop="purchaseorgname">
                        <el-input v-model="model.purchaseorgname" :readonly="true" class="is-disabled"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <!-- <el-col :span="12">
                    <el-form-item label="币种" prop="currency" :rules="rules.validateRequire">
                        <el-select v-model="model.currency" filterable clearable>
                            <el-option v-for="item in codeModel.currencyList" :key="item.code" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-col> -->
                <el-col :span="12">
                    <el-form-item label="申请支付金额" prop="money" :rules="rules.validateRequire">
                        <el-input-number v-model="model.money" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%"></el-input-number>
                        <span>元</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="费用类型" prop="costtype" :rules="rules.validateRequire">
                        <el-select v-model="model.costtype" filterable clearable>
                            <el-option v-for="item in codeModel.costtypeList" :key="item.code" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="付款方式" prop="paytype" :rules="rules.validateRequire">
                        <el-select v-model="model.paytype" filterable clearable>
                            <el-option v-for="item in codeModel.paytypeList" :key="item.code" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="收款单位" prop="rcvcompany" :rules="rules.validateRequire">
                        <el-input v-model="model.rcvcompany"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="开户行" prop="openbank" :rules="rules.validateRequire">
                        <el-input v-model="model.openbank"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="账号" prop="account" :rules="rules.validateRequire">
                        <el-input v-model="model.account"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="付款原因" prop="reason" :rules="rules.validateRequire">
                        <el-input v-model="model.reason" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="付款备注" prop="remarks">
                        <el-input v-model="model.remarks" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注" prop="description">
                        <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-collapse>
                <el-collapse-item title="【单据】相关信息">
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="申请公司" prop="orgname">
                                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="申请部门" prop="deptname" :required="true" :rules="rules.validateRequire">
                                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" dataType="code" nameType="fullname" disabled :singleSelect="true" @confirm="onSelectDept" :externalQueryParam="{ qry_enabled: 1 }"></tl-ref-dept>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="申请人姓名" prop="username" :required="true" :rules="rules.validateRequire">
                                <tl-ref-user v-model="model.usercode" :initDisplayData.sync="model.username" disabled dataType="code" :singleSelect="true"></tl-ref-user>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="申请人工号" prop="usercode">
                                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="单据日期" prop="docdate">
                                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd" :readonly="true" class="is-disabled"></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="提交时间" prop="submitdate">
                                <el-date-picker v-model="model.submitdate" type="datetime" placeholder="提交日期" value-format="yyyy-MM-dd HH:mm:ss" disabled></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="审批流程" prop="flowid">
                                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="状态" prop="state">
                                <el-input v-model="model.dispstate" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-collapse-item>
            </el-collapse>
        </el-form>
        <tl-approval-history ref="history" sourceType="PmPurchasePayment" :sourceId="model.id" />
        <div slot="footer" class="dialog-footer">
            <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" v-if="uiOperateState<=2">取消</el-button>
            <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave">
                <svg-icon icon-class="save" />保存</el-button>
            <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
                <svg-icon icon-class="send" />提交审批</el-button>
            <tl-attachment sourceType="PmPurchasePayment" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
            <!-- <tl-attachment ref="attachmentRef" sourceType="PmPaymentVouchers" :sourceId="model.id" btnTitle="付款单附件" :disabled="disableAttachment" :editable="!stateEdit" ></tl-attachment> -->
            <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
                <svg-icon icon-class="list" />审批历史</el-button>
            <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
                <svg-icon icon-class="undo" />取消审批</el-button>
        </div>
        <tlpm-purchase-contract-ref ref="pmPurchaseContractRef" @confirm="getpmPurchaseContractRefData" :isSingleSelect="true"></tlpm-purchase-contract-ref>
    </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from "@tapui/mixins/mixin_approval_edit"
/** 子组件 */
import tlpmPurchaseContractRef from "@/views/ProjectManage/pmPurchaseContract/refList"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from "@tapui/utils/http"
/** VUE配置 */
export default {
    mixins: [mixin_approval_edit],
    components: { tlpmPurchaseContractRef },
    data() {
        return {
            moduleTitle: "采购付款申请",
            urlPrefix: "/pm/purchasePayment/",
            url: {
                // 额外的url
                getProjectById: "/pm/purchasePayment/getProjectById",
                queryOrgByDeptId: "/pm/resourceArchives/queryOrgByDeptId",
                getContractAndProjectByContractId: "/pm/purchasePayment/getContractAndProjectByContractId"
            }
        }
    },
    methods: {
        assignDefaultValue() {
            if (this.isAdd) {
                this.$set(this.model, "orgcode", this.org.code)
                this.$set(this.model, "orgname", this.org.name)
                this.$set(this.model, "deptcode", this.dept.code)
                this.$set(this.model, "deptname", this.dept.fullname)
                this.$set(this.model, "usercode", this.user.code)
                this.$set(this.model, "username", this.user.name)
            }
        },
        loaded() {
            // 页面加载完毕执行
            if (!this.isAdd && this.model.docno) {
                this.title = this.title + `（单据编号：${this.model.docno}）`
            }
            let { paydate } = this.model
            if (!paydate) {
                paydate = new Date()
            }
            this.$set(this.model, "paydate", paydate)
        },
        /** 打开采购合同参照 */
        openpmPurchaseContractRefDialog() {
            this.$refs.pmPurchaseContractRef.visible = true
        },
        /** 获取采购合同参数数据 */
        getpmPurchaseContractRefData(selectionRow, selectKey, selectionValue) {
            // 合同相关信息赋值
            this.$set(this.model, "purchasecontractid", selectionRow.id)
            this.$set(this.model, "disppurchasecontractid", selectionRow.contractname)
            this.$set(this.model, "contractmoney", selectionRow.contractmoney)
            this.$set(this.model, "contractcontent", selectionRow.contractcontent)
            this.$set(this.model, "contracttotalpaymoney", selectionRow.totalpaymoney)
            const param = {
                projectId: selectionRow.projectid
            }
            getAction(this.url.getProjectById, param).then((res) => {
                if (res.success) {
                    const { id, name, code, outsourcecontractamount, projectamount, outsourcepaymentamount, receivablesamount } = res.result
                    this.$set(this.model, "projectid", id)
                    this.$set(this.model, "dispprojectid", name)
                    this.$set(this.model, "projectCode", code)
                    this.$set(this.model, "projectoutsourcecontractamount", outsourcecontractamount)
                    this.$set(this.model, "projectamount", projectamount)
                    this.$set(this.model, "projectoutsourcepaymentamount", outsourcepaymentamount)
                    this.$set(this.model, "projectreceivablesamount", receivablesamount)
                } else {
                    this.$tap.alert("查询关联项目信息错误，请联系系统管理员")
                }
            })
        },
        onSelectPurchasedept(selectedDepts) {
            const selectedDept = selectedDepts[0]
            const param = {
                deptId: selectedDept.id
            }
            getAction(this.url.queryOrgByDeptId, param).then((res) => {
                if (res.success) {
                    const { code: orgCode, name: orgName } = res.result
                    this.$set(this.model, "purchaseorgcode", orgCode)
                    this.$set(this.model, "purchaseorgname", orgName)
                } else {
                    this.$tap.alert("已选部门关联公司信息错误，请联系系统管理员")
                }
            })
        },
        onSelectDept(selectedDepts) {
            const selectedDept = selectedDepts[0]
            const param = {
                deptId: selectedDept.id
            }
            getAction(this.url.queryOrgByDeptId, param).then((res) => {
                if (res.success) {
                    const { code: orgCode, name: orgName } = res.result
                    this.$set(this.model, "orgcode", orgCode)
                    this.$set(this.model, "orgname", orgName)
                } else {
                    this.$tap.alert("已选部门关联公司信息错误，请联系系统管理员")
                }
            })
        },
        /** 提交前执行 */
        beforeSubmit() {
            // 申请金额 采购合同金额 采购合同已付款金额
            const { money, contractmoney, contracttotalpaymoney } = this.model
            if ((money || money === 0) && (contractmoney || contractmoney === 0) && (contracttotalpaymoney || contracttotalpaymoney === 0)) {
                if (money > contractmoney) {
                    this.$tap.alert("申请支付金额不能高于采购合同金额", "warning")
                    return false
                }
                if (money + contracttotalpaymoney > contractmoney) {
                    this.$tap.alert("申请支付金额与采购合同已付款金额之和不能高于采购合同金额", "warning")
                    return false
                }
            }
            // 项目采购合同金额 项目采购付款金额 项目已回款金额
            const { projectoutsourcecontractamount, projectoutsourcepaymentamount, projectreceivablesamount } = this.model
            if ((money || money === 0) && (projectoutsourcecontractamount || projectoutsourcecontractamount === 0) && (projectoutsourcepaymentamount || projectoutsourcepaymentamount === 0)) {
                if (money + projectoutsourcepaymentamount > projectoutsourcecontractamount) {
                    this.$tap.alert("申请支付金额与采购付款金额之和不能高于项目采购合同金额", "warning")
                    return false
                }
            }
            if ((money || money === 0) && (projectreceivablesamount || projectreceivablesamount === 0) && (projectoutsourcepaymentamount || projectoutsourcepaymentamount === 0)) {
                if (money + projectoutsourcepaymentamount > projectreceivablesamount) {
                    this.$tap.alert("申请支付金额与采购付款金额之和不能高于项目已回款金额", "warning")
                    return false
                }
            }
            return true
        },
        savePaymentInfo(resolve, reject) {
            const { id, paydate } = this.model
            if (!paydate) {
                this.$tap.alert("请填写付款日期")
                reject()
            }
            // if (this.$refs.attachmentRef.fileList.length === 0) {
            //     this.$tap.alert('请上传付款单附件！', 'warning')
            //     reject()
            //     return
            // }
            const param = {
                id,
                paydate
            }
            putAction(this.url.edit, param)
                .then((res) => {
                    if (res.success) {
                        resolve()
                    } else {
                        this.$tap.alert(res.message, "warning")
                        reject()
                    }
                })
                .catch((err) => {
                    this.$tap.alert("保存付款日期失败，请联系系统管理员", "warning")
                    reject()
                })
        }
    }
}
</script>
